{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum 69 Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maximum69Number "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #6 和 9 组成的最大数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个仅由数字 6 和 9 组成的正整数&nbsp;<code>num</code>。</p>\n",
    "\n",
    "<p>你最多只能翻转一位数字，将 6 变成&nbsp;9，或者把&nbsp;9 变成&nbsp;6 。</p>\n",
    "\n",
    "<p>请返回你可以得到的最大数字。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 9669\n",
    "<strong>输出：</strong>9969\n",
    "<strong>解释：</strong>\n",
    "改变第一位数字可以得到 6669 。\n",
    "改变第二位数字可以得到 9969 。\n",
    "改变第三位数字可以得到 9699 。\n",
    "改变第四位数字可以得到 9666 。\n",
    "其中最大的数字是 9969 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 9996\n",
    "<strong>输出：</strong>9999\n",
    "<strong>解释：</strong>将最后一位从 6 变到 9，其结果 9999 是最大的数。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>num = 9999\n",
    "<strong>输出：</strong>9999\n",
    "<strong>解释：</strong>无需改变就已经是最大的数字了。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= num &lt;= 10^4</code></li>\n",
    "\t<li><code>num</code>&nbsp;每一位上的数字都是 6 或者&nbsp;9 。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-69-number](https://leetcode.cn/problems/maximum-69-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-69-number](https://leetcode.cn/problems/maximum-69-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['9669', '9996', '9999']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        numToString = str(num)\n",
    "        for i in range(len(numToString)) :\n",
    "            if numToString[i] == \"6\":\n",
    "                numToString = numToString[:i] + \"9\" +numToString[i+1:]\n",
    "                break\n",
    "\n",
    "        return int(numToString)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        nums = []\n",
    "        while num > 0:\n",
    "            nums.append(num%10)\n",
    "            num //= 10\n",
    "        for i in range(len(nums)-1,-1,-1):\n",
    "            if nums[i] == 6:\n",
    "                nums[i] = 9\n",
    "                break\n",
    "        # print(nums)\n",
    "        res = 0\n",
    "        for i in nums[::-1]:\n",
    "            res = res*10 + i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        res=int(str(num).replace('6','9',1))\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace('6','9',1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        nums = list(s)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] == \"6\":\n",
    "                nums[i] = \"9\"\n",
    "                break\n",
    "        s = ''.join(nums)\n",
    "        return int(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        num = str(num)\n",
    "        s = ''\n",
    "        for i in range(len(num)):\n",
    "            if num[i] == '6':\n",
    "                s += '9'\n",
    "                s += num[i + 1:]\n",
    "                break\n",
    "            else:\n",
    "                s += num[i]\n",
    "        return int(s)\n",
    "                \n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        i = s.find('6')\n",
    "        if i == -1:\n",
    "            return num\n",
    "        \n",
    "        return int(s[0:i:1] + '9' + s[i+1::])\n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        a = [ int(i) for i in str(num)]\n",
    "        for k,v in enumerate(a):\n",
    "            if v==6:\n",
    "                a[k] = 9\n",
    "                break\n",
    "        b = ''\n",
    "        for i in a:\n",
    "            b +=  str(i)\n",
    "        return int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace('6', '9', 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '6':\n",
    "                s = s[:i] + '9' +s[i+1:]\n",
    "                return int(s)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        cache = list(str(num))\n",
    "        for i in range(len(cache)):\n",
    "            if cache[i] == '6':\n",
    "                cache[i] = '9'\n",
    "                break\n",
    "        return int(''.join(cache))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        res = ''\n",
    "        cnt = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '6' and cnt == 0:\n",
    "                res += '9'\n",
    "                cnt += 1\n",
    "            else:\n",
    "                res += s[i]\n",
    "        return int(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        string = str(num)\n",
    "        string = string.replace('6', '9', 1)\n",
    "        return int(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        try:\n",
    "            i=s.index('6')\n",
    "            return int(s[:i]+'9'+s[i+1:])\n",
    "        except:\n",
    "            return int(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        nums = []\n",
    "        while num:\n",
    "            nums.append(num % 10)\n",
    "            num //= 10\n",
    "\n",
    "        n, used, ans = len(nums), False, 0\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            if nums[i] == 6 and not used:\n",
    "                ans = ans * 10 + 9\n",
    "                used = True\n",
    "            else:\n",
    "                ans = ans * 10 + nums[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        ans = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '6':\n",
    "                ans += '9'\n",
    "                break\n",
    "            else:\n",
    "                ans += '9'\n",
    "        if len(ans) < len(s):\n",
    "            return int(ans + s[len(ans):])\n",
    "        else:\n",
    "            return int(ans)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s=list(str(num))\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='6':\n",
    "                s[i]='9'\n",
    "                break        \n",
    "        return int(''.join(s))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        for i in range(3, -1, -1):\n",
    "            if num // 10**i % 10 == 6:\n",
    "                return num + 3 * 10**i\n",
    "        return num\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "       return int(str(num).replace(\"6\",\"9\",1))\n",
    "        \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        if num // 1000  == 6: return num + 3000\n",
    "        if num // 100 % 10 == 6: return num + 300\n",
    "        if num // 10  % 10 == 6: return num + 30\n",
    "        if num        % 10 == 6: return num + 3\n",
    "        return num\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        list1 = list(str(num))\n",
    "        for i in range(len(list1)):\n",
    "            if list1[i]=='6':\n",
    "                list1[i] = '9'\n",
    "                break\n",
    "        return int(''.join(list1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        list1 = list(str(num))\n",
    "        for i in range(len(list1)):\n",
    "            if list1[i]=='6':\n",
    "                list1[i] = '9'\n",
    "                break\n",
    "        return int(''.join(list1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        num=list(str(num))\n",
    "        for i in range(len(num)):\n",
    "            if num[i]=='6':\n",
    "                num[i]='9'\n",
    "                break\n",
    "        return int(''.join(num))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        numbers = []\n",
    "        while num:\n",
    "            numbers.append(num%10)\n",
    "            num //= 10\n",
    "        numbers.reverse()\n",
    "        print(numbers)\n",
    "        for i in range(len(numbers)):\n",
    "            if numbers[i] == 6:\n",
    "                numbers[i] = 9\n",
    "                break\n",
    "        ans = numbers[0]\n",
    "        for i in range(1,len(numbers)):\n",
    "            ans = ans*10 + numbers[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        res=''\n",
    "        flag=True\n",
    "        for i in s:\n",
    "            if i=='6' and flag:\n",
    "                res+='9'\n",
    "                flag=False\n",
    "            else:\n",
    "                res+=i\n",
    "        return int(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        strnum = list(str(num))\n",
    "        for i in range(len(strnum)):\n",
    "            if strnum[i] == '6':\n",
    "                return num + 3*10**(len(strnum)-i-1)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\",\"9\",1))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        # num = list(str(num))\n",
    "        # try:\n",
    "        #     num[num.index('6')] = '9'\n",
    "        # except:\n",
    "        #     pass\n",
    "        # return int(''.join(str(s) for s in num))\n",
    "        return int(str(num).replace('6', '9', 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        num = list(str(num))\n",
    "        for i in range(len(num)):\n",
    "            if num[i] == '6':\n",
    "                num[i] = '9'\n",
    "                break\n",
    "        return int(''.join(num))\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        w1 = s.replace('6', '9', 1)\n",
    "        return int(w1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        a=str(num).replace('6','9',1)\n",
    "        return int(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        new = list(str(num))\n",
    "        for i in range(len(new)):\n",
    "            if new[i]=='6':\n",
    "                new[i]='9'\n",
    "                return int(''.join(new))\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        # s = [int(_) for _ in list(str(num))]\n",
    "        # for i, ele in enumerate(s):\n",
    "        #     if s == 6:\n",
    "        s = str(num)\n",
    "        if '6' not in s:\n",
    "            return num\n",
    "        else:\n",
    "            idx = s.index('6')\n",
    "            return int(s[:idx]+'9'+s[idx+1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace('6','9',1))\n",
    "        # new = list(str(num))\n",
    "        # for i in range(len(new)):\n",
    "        #     if new[i]=='6':\n",
    "        #         new[i]='9'\n",
    "        #         return int(''.join(new))\n",
    "        # return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        ge = num % 10\n",
    "        shi = num % 100 // 10\n",
    "        bai = num % 1000 // 100\n",
    "        qian = num // 1000\n",
    "        number = [qian, bai, shi, ge]\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == 6:\n",
    "                number[i] = 9\n",
    "                break\n",
    "        return number[0] * 1000 + number[1] * 100 + number[2] * 10 + number[3]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        ans = \"\"\n",
    "        ok = True\n",
    "        for i, x in enumerate(str(num)):\n",
    "            if x == \"6\" and ok:\n",
    "                ans += \"9\"\n",
    "                ok = False\n",
    "            else:\n",
    "                ans += x\n",
    "        return int(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        num = list(str(num))\n",
    "        #print(num)\n",
    "        for i in range(0,len(num)):\n",
    "            if num[i] == str(6):\n",
    "                num[i] = str(9)\n",
    "                break\n",
    "        temp = ''\n",
    "        for i in range(0,len(num)):\n",
    "            temp += num[i]\n",
    "        return int(temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace('6', '9', 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        li = list()\n",
    "        while num > 0:\n",
    "            li.append(num % 10)\n",
    "            num //= 10\n",
    "        i = len(li) - 1\n",
    "        while i >= 0:\n",
    "            if li[i] == 6:\n",
    "                li[i] = 9\n",
    "                break\n",
    "            i -= 1\n",
    "        i, num = len(li) - 1, 0\n",
    "        while i >= 0:\n",
    "            num = num * 10 + li[i]\n",
    "            i -= 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\",\"9\",1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = ''\n",
    "        for i in range(len(str(num))):\n",
    "            if str(num)[i] == '6':\n",
    "                return int(str(num)[:i] + '9' + str(num)[i+1:])\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        # s = str(num)\n",
    "\n",
    "        return int(str(num).replace('6','9',1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        str1 = str(num)\n",
    "        lock = 0\n",
    "        str2 = \"\"\n",
    "        for i in range(len(str1)):\n",
    "            if str1[i] != '9' and lock == 0:\n",
    "                str2 += \"9\"\n",
    "                lock += 1\n",
    "            else:\n",
    "                str2 += str1[i]\n",
    "        return int(str2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        p=str(num)\n",
    "        q=[i for i in p]\n",
    "        t=''\n",
    "        for i in range(len(q)):\n",
    "            if q[i]=='6':\n",
    "                q[i]='9'\n",
    "                break\n",
    "            else:\n",
    "                continue\n",
    "        for i in range(len(q)):\n",
    "            t+=q[i]\n",
    "        return int(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        str_num = str(num)\n",
    "        if str_num.find('6') == -1:\n",
    "            return num\n",
    "        else:\n",
    "            mi = len(str_num)-str_num.find('6')-1\n",
    "            return num + 3*10**mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        n = str(num)\n",
    "        for i in range(len(n)):\n",
    "            if n[i] == '6':\n",
    "                break\n",
    "        return int(n[:i]+'9'+n[i+1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        ns=list(str(num))\n",
    "        for i in range(len(ns)):\n",
    "            if ns[i]=='6':\n",
    "                ns[i]='9'\n",
    "                break\n",
    "        return int(''.join(ns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        a = num\n",
    "        for i in range(len(s)):\n",
    "            s = str(num)\n",
    "            if s[i] == '6':\n",
    "                sl = list(s)\n",
    "                sl[i] = '9'\n",
    "                s = ''.join(sl)\n",
    "            # else:\n",
    "            #     sl = list(s)\n",
    "            #     sl[i]='6'\n",
    "            #     s = ''.join(sl)\n",
    "            if int(s)> a:\n",
    "                a = int(s)\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def maximum69Number (self, num: int) -> int:\n",
    "#         num=[int(d) for d in list(str(num))]\n",
    "\n",
    "#         for i,d in enumerate(num):\n",
    "#             if d==6:\n",
    "#                 num[i]=9\n",
    "#                 break\n",
    "\n",
    "#         return int(\"\".join(str(d) for d in num))\n",
    "\n",
    "\n",
    "## 官码：贪心\n",
    "class Solution:\n",
    "    def maximum69Number(self, num: int) -> int:\n",
    "        return int(str(num).replace(\"6\", \"9\", 1))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        n=list(str(num))\n",
    "        for i in range(len(n)):\n",
    "            if n[i]=='6':\n",
    "                n[i]='9'\n",
    "                break\n",
    "        return int(''.join(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        s = str(num)\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == '6':\n",
    "                num += 3 * 10 ** (len(s) - i - 1)\n",
    "                return num\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        l=list(str(num))\n",
    "        for i,x in enumerate(l):\n",
    "            if x=='6':\n",
    "                l[i]='9'\n",
    "                break\n",
    "        return int(''.join(l))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "         return int(str(num).replace('6', '9', 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        result = list(str(num))\n",
    "        n , flag = len(result) , True\n",
    "        for i in range(n):\n",
    "            if result[i] == '6' and flag:\n",
    "                result[i] = '9'\n",
    "                flag = False\n",
    "                break\n",
    "        return int(\"\".join(result))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        num = str(num)\n",
    "\n",
    "        num = num.replace(\"6\", \"9\", 1)\n",
    "\n",
    "        return int(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        ls = [x for x in str(num)]\n",
    "        for i in range(len(ls)):\n",
    "            if ls[i] != '9':\n",
    "                ls[i] = '9'\n",
    "                break\n",
    "        return int(''.join(ls))\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximum69Number (self, num: int) -> int:\n",
    "        numstr = str(num)\n",
    "        n = len(numstr)\n",
    "        for i in range(n):\n",
    "            if numstr[i] == '6':\n",
    "                num += 3 * 10**(n - i - 1)\n",
    "                break\n",
    "        return num"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
